2000W / 48V LLC Battery-Charger  V1.00
xmc_i2c.c
Go to the documentation of this file.
1 
61 /*********************************************************************************************************************
62  * HEADER FILES
63  *********************************************************************************************************************/
64 #include <xmc_i2c.h>
65 
66 /*********************************************************************************************************************
67  * MACROS
68  *********************************************************************************************************************/
69 #define XMC_I2C_7BIT_ADDR_Pos (8U)
70 #define TRANSMISSION_MODE (3U)
73 #define WORDLENGTH (7U)
74 #define SET_TDV (1U)
75 #define XMC_I2C_10BIT_ADDR_MASK (0x7C00U)
77 /*********************************************************************************************************************
78  * ENUMS
79  *********************************************************************************************************************/
80 
81 typedef enum XMC_I2C_CH_TDF
82 {
84  XMC_I2C_CH_TDF_SLAVE_SEND = (uint32_t)1U << 8U,
85  XMC_I2C_CH_TDF_MASTER_RECEIVE_ACK = (uint32_t)2U << 8U,
86  XMC_I2C_CH_TDF_MASTER_RECEIVE_NACK = (uint32_t)3U << 8U,
87  XMC_I2C_CH_TDF_MASTER_START = (uint32_t)4U << 8U,
88  XMC_I2C_CH_TDF_MASTER_RESTART = (uint32_t)5U << 8U,
89  XMC_I2C_CH_TDF_MASTER_STOP = (uint32_t)6U << 8U
91 
92 typedef enum XMC_I2C_CH_MAX_SPEED
93 {
97 
99 {
104 /*********************************************************************************************************************
105  * API IMPLEMENTATION
106  *********************************************************************************************************************/
107 /* Initializes the USIC channel by setting the data format, slave address, baudrate, transfer buffer */
108 void XMC_I2C_CH_Init(XMC_USIC_CH_t *const channel, const XMC_I2C_CH_CONFIG_t *const config)
109 {
111 
112  /* Data format configuration */
113  channel->SCTR = ((uint32_t)TRANSMISSION_MODE << (uint32_t)USIC_CH_SCTR_TRM_Pos) | /* Transmision mode */
114  ((uint32_t)WORDLENGTH << (uint32_t)USIC_CH_SCTR_WLE_Pos) | /* 8 data bits */
115  USIC_CH_SCTR_FLE_Msk | /* unlimited data flow */
116  USIC_CH_SCTR_SDIR_Msk | /* MSB shifted first */
117  USIC_CH_SCTR_PDL_Msk; /* Passive Data Level */
118 
119  XMC_I2C_CH_SetSlaveAddress(channel, config->address);
120  (void)XMC_I2C_CH_SetBaudrate(channel, config->baudrate);
121 
122  /* Enable transfer buffer */
123  channel->TCSR = ((uint32_t)SET_TDV << (uint32_t)USIC_CH_TCSR_TDEN_Pos) | USIC_CH_TCSR_TDSSM_Msk;
124 
125  /* Clear status flags */
126  channel->PSCR = 0xFFFFFFFFU;
127 
128  /* Disable parity generation */
129  channel->CCR = 0x0U;
130 }
131 /* Sets the slave address */
132 void XMC_I2C_CH_SetSlaveAddress(XMC_USIC_CH_t *const channel, const uint16_t address)
133 {
135  {
136  channel->PCR_IICMode = (address & 0xffU) | ((address << 1) & 0xfe00U);
137  }
138  else
139  {
140  channel->PCR_IICMode = ((uint32_t)address) << XMC_I2C_7BIT_ADDR_Pos;
141  }
142 }
143 /* Read the slave address */
144 uint16_t XMC_I2C_CH_GetSlaveAddress(const XMC_USIC_CH_t *const channel)
145 {
146  uint32_t address = channel->PCR_IICMode & (uint32_t)USIC_CH_PCR_IICMode_SLAD_Msk;
147 
148  if ((address & 0xffU) == 0U)
149  {
150  address = address >> XMC_I2C_7BIT_ADDR_Pos;
151  }
152  else
153  {
154  address = (address & 0xffU) | ((address >> 1) & 0x0300U);
155  }
156 
157  return (uint16_t)address;
158 }
159 /* Sets the baudrate and oversampling based on standard speed or fast speed */
160 XMC_I2C_CH_STATUS_t XMC_I2C_CH_SetBaudrate(XMC_USIC_CH_t *const channel, uint32_t rate)
161 {
163 
164  status = XMC_I2C_CH_STATUS_ERROR;
165 
166  if (rate <= (uint32_t)XMC_I2C_CH_MAX_SPEED_STANDARD)
167  {
168  channel->PCR_IICMode &= (uint32_t)~USIC_CH_PCR_IICMode_STIM_Msk;
170  {
171  status = XMC_I2C_CH_STATUS_OK;
172  }
173  }
174  else if (rate <= (uint32_t)XMC_I2C_CH_MAX_SPEED_FAST)
175  {
176  channel->PCR_IICMode |= (uint32_t)USIC_CH_PCR_IICMode_STIM_Msk;
178  {
179  status = XMC_I2C_CH_STATUS_OK;
180  }
181  }
182  else
183  {
184  status = XMC_I2C_CH_STATUS_ERROR;
185  }
186 
187  return status;
188 }
189 /* Sends master start condition along with read/write command to IN/TBUF register based on FIFO/non-FIFO modes. */
190 void XMC_I2C_CH_MasterStart(XMC_USIC_CH_t *const channel, const uint16_t addr, const XMC_I2C_CH_CMD_t command)
191 {
192  uint32_t temp;
193 
194  temp = addr | (uint32_t)XMC_I2C_CH_TDF_MASTER_START;
195  if (command == XMC_I2C_CH_CMD_READ)
196  {
197  temp |= 0x1U;
198  }
199 
200  /* Check FIFO size */
201  if ((channel->TBCTR & USIC_CH_TBCTR_SIZE_Msk) == 0U)
202  {
204  {
205  /* check TDV, wait until TBUF is ready */
206  }
207 
208  /* clear PSR_TBIF */
210 
211  channel->TBUF[0] = temp;
212  }
213  else
214  {
215  channel->IN[0U] = temp;
216  }
217 }
218 /* Sends master repeated start condition along with read/write command to IN/TBUF register based on FIFO/non-FIFO modes. */
219 void XMC_I2C_CH_MasterRepeatedStart(XMC_USIC_CH_t *const channel, const uint16_t addr, const XMC_I2C_CH_CMD_t command)
220 {
221  uint32_t tmp;
222  tmp = addr | (uint32_t)XMC_I2C_CH_TDF_MASTER_RESTART;
223  if (command == XMC_I2C_CH_CMD_READ)
224  {
225  tmp |= 0x1U;
226  }
227 
228  /* Check FIFO size */
229  if ((channel->TBCTR & USIC_CH_TBCTR_SIZE_Msk) == 0U)
230  {
232  {
233  /* check TDV, wait until TBUF is ready */
234  }
235 
236  /* clear PSR_TBIF */
238 
239  channel->TBUF[0] = tmp;
240  }
241  else
242  {
243  channel->IN[0U] = tmp;
244  }
245 }
246 
247 /* Sends master stop command to IN/TBUF register based on FIFO/non-FIFO modes. */
248 void XMC_I2C_CH_MasterStop(XMC_USIC_CH_t *const channel)
249 {
250  /* Check FIFO size */
251  if ((channel->TBCTR & USIC_CH_TBCTR_SIZE_Msk) == 0U)
252  {
254  {
255  /* check TDV, wait until TBUF is ready */
256  }
257 
258  /* clear PSR_TBIF */
260 
261  channel->TBUF[0] = (uint32_t)XMC_I2C_CH_TDF_MASTER_STOP;
262  }
263  else
264  {
265  channel->IN[0U] = (uint32_t)XMC_I2C_CH_TDF_MASTER_STOP;
266  }
267 }
268 
269 /* Sends master send command along with data to IN/TBUF register based on FIFO/non-FIFO modes. */
270 void XMC_I2C_CH_MasterTransmit(XMC_USIC_CH_t *const channel, const uint8_t data)
271 {
272  /* Check FIFO size */
273  if ((channel->TBCTR & USIC_CH_TBCTR_SIZE_Msk) == 0U)
274  {
276  {
277  /* check TDV, wait until TBUF is ready */
278  }
279 
280  /* clear PSR_TBIF */
282 
283  channel->TBUF[0] = (uint32_t)XMC_I2C_CH_TDF_MASTER_SEND | data;
284  }
285  else
286  {
287  channel->IN[0] = (uint32_t)XMC_I2C_CH_TDF_MASTER_SEND | data;
288  }
289 }
290 
291 /* Sends slave send command along with data to IN/TBUF register based on FIFO/non-FIFO modes. */
292 void XMC_I2C_CH_SlaveTransmit(XMC_USIC_CH_t *const channel, const uint8_t data)
293 {
294  /* Check FIFO size */
295  if ((channel->TBCTR & USIC_CH_TBCTR_SIZE_Msk) == 0U)
296  {
298  {
299  /* check TDV, wait until TBUF is ready */
300  }
301 
302  /* clear PSR_TBIF */
304 
305  channel->TBUF[0] = (uint32_t)XMC_I2C_CH_TDF_SLAVE_SEND | data;
306  }
307  else
308  {
309  channel->IN[0] = (uint32_t)XMC_I2C_CH_TDF_SLAVE_SEND | data;
310  }
311 }
312 
313 /* Sends master receive ack command to IN/TBUF register based on FIFO/non-FIFO modes. */
314 void XMC_I2C_CH_MasterReceiveAck(XMC_USIC_CH_t *const channel)
315 {
316 /* Check FIFO size */
317  if ((channel->TBCTR & USIC_CH_TBCTR_SIZE_Msk) == 0U)
318  {
320  {
321  /* check TDV, wait until TBUF is ready */
322  }
323 
324  /* clear PSR_TBIF */
326 
327  channel->TBUF[0] = (uint32_t)XMC_I2C_CH_TDF_MASTER_RECEIVE_ACK;
328  }
329  else
330  {
331  channel->IN[0] = (uint32_t)XMC_I2C_CH_TDF_MASTER_RECEIVE_ACK;
332  }
333 }
334 
335 /* Sends master receive nack command to IN/TBUF register based on FIFO/non-FIFO modes. */
336 void XMC_I2C_CH_MasterReceiveNack(XMC_USIC_CH_t *const channel)
337 {
338  /* Check FIFO size */
339  if ((channel->TBCTR & USIC_CH_TBCTR_SIZE_Msk) == 0U)
340  {
342  {
343  /* check TDV, wait until TBUF is ready */
344  }
345 
346  /* clear PSR_TBIF */
348 
349  channel->TBUF[0] = (uint32_t)XMC_I2C_CH_TDF_MASTER_RECEIVE_NACK;
350  }
351  else
352  {
353  channel->IN[0] = (uint32_t)XMC_I2C_CH_TDF_MASTER_RECEIVE_NACK;
354  }
355 }
356 
357 /* Reads the data from RBUF if FIFO size is 0 otherwise from OUTR. */
358 uint8_t XMC_I2C_CH_GetReceivedData(const XMC_USIC_CH_t *const channel)
359 {
360  uint8_t retval;
361 
362  /* Check FIFO size */
363  if ((channel->RBCTR & USIC_CH_RBCTR_SIZE_Msk) == 0U)
364  {
365  retval = (uint8_t)channel->RBUF;
366  }
367  else
368  {
369  retval = (uint8_t)channel->OUTR;
370  }
371 
372  return retval;
373 }
374 
375 /* Sets the operating mode of USIC to IDLE */
377 {
379 
380  if (((uint32_t)XMC_USIC_CH_GetTransmitBufferStatus(channel) & (uint32_t)XMC_USIC_CH_TBUF_STATUS_BUSY) != 0U)
381  {
382  status = XMC_I2C_CH_STATUS_BUSY;
383  }
384  else
385  {
386  /* USIC channel in IDLE mode */
388  }
389  return status;
390 }
391 
392 void XMC_I2C_CH_EnableEvent(XMC_USIC_CH_t *const channel, const uint32_t event)
393 {
394  channel->CCR |= (event&0x1fc00U);
395  channel->PCR_IICMode |= ((event) & 0x41fc0000U);
396 }
397 
398 void XMC_I2C_CH_DisableEvent(XMC_USIC_CH_t *const channel, const uint32_t event)
399 {
400  channel->CCR &= (uint32_t)~(event&0x1fc00U);
401  channel->PCR_IICMode &= (uint32_t)~((event) & 0x41fc0000U);
402 }
XMC_I2C_CH_TDF
Definition: xmc_i2c.c:83
enum XMC_I2C_CH_MAX_SPEED XMC_I2C_CH_MAX_SPEED_t
#define XMC_I2C_7BIT_ADDR_Pos
Definition: xmc_i2c.c:69
__STATIC_INLINE void XMC_USIC_CH_SetMode(XMC_USIC_CH_t *const channel, const XMC_USIC_CH_OPERATING_MODE_t mode)
Definition: xmc_usic.h:2076
enum XMC_I2C_CH_STATUS XMC_I2C_CH_STATUS_t
I2C Status.
__IO uint32_t TBUF[32]
Definition: xmc_usic.h:523
I2C_CH configuration structure.
Definition: xmc_i2c.h:256
void XMC_I2C_CH_SetSlaveAddress(XMC_USIC_CH_t *const channel, const uint16_t address)
Definition: xmc_i2c.c:134
__STATIC_INLINE XMC_USIC_CH_TBUF_STATUS_t XMC_USIC_CH_GetTransmitBufferStatus(XMC_USIC_CH_t *const channel)
Definition: xmc_usic.h:1182
#define USIC_CH_TCSR_TDEN_Pos
Definition: XMC1400.h:2993
__O uint32_t IN[32]
Definition: xmc_usic.h:534
uint16_t XMC_I2C_CH_GetSlaveAddress(const XMC_USIC_CH_t *const channel)
Definition: xmc_i2c.c:146
uint32_t baudrate
Definition: xmc_i2c.h:258
XMC_USIC_CH_STATUS_t XMC_USIC_CH_SetBaudrate(XMC_USIC_CH_t *const channel, uint32_t rate, uint32_t oversampling)
Definition: xmc_usic.c:120
#define TRANSMISSION_MODE
Definition: xmc_i2c.c:70
__IO uint32_t CCR
Definition: xmc_usic.h:504
void XMC_I2C_CH_Init(XMC_USIC_CH_t *const channel, const XMC_I2C_CH_CONFIG_t *const config)
Definition: xmc_i2c.c:110
void XMC_I2C_CH_MasterStart(XMC_USIC_CH_t *const channel, const uint16_t addr, const XMC_I2C_CH_CMD_t command)
Definition: xmc_i2c.c:192
#define USIC_CH_TCSR_TDSSM_Msk
Definition: XMC1400.h:2992
XMC_I2C_CH_STATUS_t XMC_I2C_CH_Stop(XMC_USIC_CH_t *const channel)
Definition: xmc_i2c.c:378
uint8_t XMC_I2C_CH_GetReceivedData(const XMC_USIC_CH_t *const channel)
Definition: xmc_i2c.c:360
#define USIC_CH_SCTR_TRM_Pos
Definition: XMC1400.h:2967
void XMC_I2C_CH_MasterStop(XMC_USIC_CH_t *const channel)
Definition: xmc_i2c.c:250
#define USIC_CH_PCR_IICMode_SLAD_Msk
Definition: XMC1400.h:3134
XMC_I2C_CH_CLOCK_OVERSAMPLING
Definition: xmc_i2c.c:100
void XMC_I2C_CH_DisableEvent(XMC_USIC_CH_t *const channel, const uint32_t event)
Definition: xmc_i2c.c:400
#define USIC_CH_PCR_IICMode_STIM_Msk
Definition: XMC1400.h:3138
__I uint32_t RBUF
Definition: xmc_usic.h:516
XMC_I2C_CH_STATUS_t XMC_I2C_CH_SetBaudrate(XMC_USIC_CH_t *const channel, uint32_t rate)
Definition: xmc_i2c.c:162
void XMC_USIC_CH_Enable(XMC_USIC_CH_t *const channel)
Definition: xmc_usic.c:79
#define USIC_CH_SCTR_FLE_Msk
Definition: XMC1400.h:2970
__I uint32_t OUTR
Definition: xmc_usic.h:531
void XMC_I2C_CH_MasterRepeatedStart(XMC_USIC_CH_t *const channel, const uint16_t addr, const XMC_I2C_CH_CMD_t command)
Definition: xmc_i2c.c:221
void XMC_I2C_CH_SlaveTransmit(XMC_USIC_CH_t *const channel, const uint8_t data)
Definition: xmc_i2c.c:294
enum XMC_I2C_CH_TDF XMC_I2C_CH_TDF_t
__IO uint32_t SCTR
Definition: xmc_usic.h:494
#define XMC_I2C_10BIT_ADDR_GROUP
Definition: xmc_i2c.h:131
__IO uint32_t TCSR
Definition: xmc_usic.h:495
#define USIC_CH_SCTR_PDL_Msk
Definition: XMC1400.h:2960
#define USIC_CH_TBCTR_SIZE_Msk
Definition: XMC1400.h:3530
__IO uint32_t TBCTR
Definition: xmc_usic.h:526
#define XMC_I2C_10BIT_ADDR_MASK
Definition: xmc_i2c.c:77
void XMC_I2C_CH_MasterReceiveNack(XMC_USIC_CH_t *const channel)
Definition: xmc_i2c.c:338
__IO uint32_t RBCTR
Definition: xmc_usic.h:527
#define SET_TDV
Definition: xmc_i2c.c:76
enum XMC_I2C_CH_CMD XMC_I2C_CH_CMD_t
I2C commands.
#define USIC_CH_SCTR_SDIR_Msk
Definition: XMC1400.h:2958
__O uint32_t PSCR
Definition: xmc_usic.h:514
#define WORDLENGTH
Definition: xmc_i2c.c:75
enum XMC_I2C_CH_CLOCK_OVERSAMPLING XMC_I2C_CH_CLOCK_OVERSAMPLINGS_t
#define USIC_CH_SCTR_WLE_Pos
Definition: XMC1400.h:2971
#define USIC_CH_RBCTR_SIZE_Msk
Definition: XMC1400.h:3554
__IO uint32_t PCR_IICMode
Definition: xmc_usic.h:498
void XMC_I2C_CH_MasterTransmit(XMC_USIC_CH_t *const channel, const uint8_t data)
Definition: xmc_i2c.c:272
void XMC_I2C_CH_MasterReceiveAck(XMC_USIC_CH_t *const channel)
Definition: xmc_i2c.c:316
void XMC_I2C_CH_EnableEvent(XMC_USIC_CH_t *const channel, const uint32_t event)
Definition: xmc_i2c.c:394
uint16_t address
Definition: xmc_i2c.h:259
XMC_I2C_CH_MAX_SPEED
Definition: xmc_i2c.c:94
__STATIC_INLINE void XMC_I2C_CH_ClearStatusFlag(XMC_USIC_CH_t *const channel, uint32_t flag)
Definition: xmc_i2c.h:676